package com.example.cloud.action;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.cloud.model.UserInfo;
import com.example.cloud.service.UserService;

/**
 * UserAction
 *
 * @author tongyufu
 */
@RestController
public class UserAction {

    @Autowired
    private UserService userService;

    /**
     * 异步调用
     *
     * @param id 用户id
     * @return 用户信息
     */
    @RequestMapping("/user/{id}")
    public UserInfo getById(@PathVariable("id") Integer id) {
        Future<UserInfo> future = userService.getUserByIdAsync(id);
        try {
            //get方法会阻塞当前线程，直到异步方法返回
            UserInfo user = future.get();
            System.out.println("-----------" + user);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return new UserInfo(id, "用户：" + id);
    }

}
